//
//  HMTreasureDetailsViewController.m
//  HeartMark
//
//  Created by rimi on 2016/11/16.
//  Copyright © 2016年 黄华强. All rights reserved.
//

#import "HMTreasureDetailsViewController.h"
#import <AVOSCloud/AVOSCloud.h>

#import "HMAlertView.h"

@interface HMTreasureDetailsViewController ()

@property (nonatomic,strong) UIImageView *iconImageView;
@property (nonatomic,strong) UILabel *authorNameLabel;
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UITextView *contentView;
@property (nonatomic,assign) BOOL isLike;
@property (nonatomic,assign) NSInteger blockNum;
@end

@implementation HMTreasureDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isLike = YES;
    _blockNum = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupUI];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupUI {
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"已收藏_32"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClicked:)];
    
    _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(12, 72, 50, 50)];
   
    AVFile *file = [AVFile fileWithURL:_likeModel.iconImageUrl];
    [file getThumbnail:YES width:100 height:100 withBlock:^(UIImage *image, NSError *error) {
        if (error) {
            _iconImageView.image = [UIImage imageNamed:@"默认头像"];
        } else {
            _iconImageView.image = image;
        }
        
    }];
    [self.view addSubview:_iconImageView];
    
    
    _authorNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(72, 72, 200, 20)];
    
    if ([_likeModel.author isEqualToString:@""]) {
        _authorNameLabel.text = @"匿名用户";
    } else {
        _authorNameLabel.text = _likeModel.author;
    }
    
    _authorNameLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:_authorNameLabel];
    
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(72, 105, 200, 15)];
    NSDateFormatter* df = [[NSDateFormatter alloc]init];//格式化
    [df setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString* s1 = [df stringFromDate:_likeModel.updatedAt];
    _timeLabel.text = s1;
    _timeLabel.font = [UIFont systemFontOfSize:13];
    _timeLabel.textColor = [UIColor lightGrayColor];
    [self.view addSubview:_timeLabel];
    
    _contentView = [[UITextView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 0.03, 150, SCREEN_WIDTH * 0.94, SCREEN_HEIGHT - 150)];
    [_contentView setEditable:NO];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8; //行距
    paragraphStyle.firstLineHeadIndent = 0;
    
    
    
    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:TEXT_FONT], NSParagraphStyleAttributeName:paragraphStyle};
    
    
    _contentView.attributedText = [[NSAttributedString alloc]initWithString: _likeModel.content attributes:attributes];
    
    [self.view addSubview:_contentView];
}






- (void)rightBtnClicked:(UIBarButtonItem *)button {
    [button setEnabled:NO];
    AVUser *currentUser = [AVUser currentUser];
    AVObject *obj = _likeModel.noteObj;
    
    [AVObject saveAllInBackground:@[obj] block:^(BOOL succeeded, NSError *error) {
        if (error) {
            [HMAlertView bottomAlertView:@"网络异常"];
        } else {
            // 保存成功
            AVRelation *relation = [currentUser relationForKey:@"collectionNote"];
            
            if (_isLike) {
                [relation removeObject:obj];
                NSDictionary *userInfo = @{@"isAdd":@"NO"};
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"collectionNotification" object:obj userInfo:userInfo];
            } else {
                [relation addObject:obj];
                NSDictionary *userInfo = @{@"isAdd":@"YES"};
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"collectionNotification" object:obj userInfo:userInfo];
                
            }
            
            [currentUser saveInBackground];
            _blockNum += 1;
            if (_blockNum == 2) {
                _blockNum = 0;
                _isLike = !_isLike;
                
                if (_isLike) {
                    [button setImage:[UIImage imageNamed:@"已收藏_32"] ];
                    [HMAlertView bottomAlertView:@"收藏成功!"];
                } else {
                    
                    [button setImage:[UIImage imageNamed:@"未收藏_32"]];
                    [HMAlertView bottomAlertView:@"取消收藏成功!"];
                }
                [button setEnabled:YES];
            }
            
        }
    }];
    
    [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
        if (error) {
            [HMAlertView bottomAlertView:@"网络异常"];
        } else {
        
        if (_isLike) {
            [obj incrementKey:@"collectNumber" byAmount:@(-1)];
        } else {
            // 原子增加查看的次数
            [obj incrementKey:@"collectNumber"];
        }
        // 保存时自动取回云端最新数据
        obj.fetchWhenSave = true;
        [obj saveInBackground];
        _blockNum += 1;
        if (_blockNum == 2) {
            _blockNum = 0;
            _isLike = !_isLike;
            
            if (_isLike) {
                [button setImage:[UIImage imageNamed:@"已收藏_32"] ];
                [HMAlertView bottomAlertView:@"收藏成功！"];
            } else {
                [HMAlertView bottomAlertView:@"取消收藏成功！"];
                [button setImage:[UIImage imageNamed:@"未收藏_32"] ];
            }
            [button setEnabled:YES];
        }
        }
    }];
    

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}

@end
